package com.eastelsoft.smartiot.bssserver.controller;

import com.eastelsoft.smartiot.bssserver.dao.*;
import com.eastelsoft.smartiot.bssserver.domain.*;
import com.eastelsoft.smartiot.bssserver.model.*;
import com.eastelsoft.smartiot.bssserver.model.ErrorJsonBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.UUID;

/**
* 数据库 [okwuyou_crawler] 中表 [T_CM_STAFF] 的Controller类.
*  
* @author: {GenerateCode}
* 创建日期: 2019/9/4
* 修 改 人: 
* 修改日期:
* 修改内容:
* 版    本: 1.0.0
*/
@SuppressWarnings("ALL")
@RestController
@RequestMapping("/api/goldsandalwood/tcmstaff")
public class TcmStaffController extends AbstractController {
    private static Logger logger = LoggerFactory.getLogger(TcmStaffController.class);
     @Autowired
    private TCmStaffMapper tCmStaffMapper;
    @Autowired
    private TCmUserAccountMapper tCmUserAccountMapper;
    /**
     * 列表
     */
    @CrossOrigin(origins = "*")
    @RequestMapping(value = "/listTCmStaff", method = RequestMethod.GET)
    public PageInfo getTCmStaffByIdListByPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        PageHelper.startPage(page,size);
        PageInfo pageInfo = new PageInfo(tCmStaffMapper.selectAll());
        logger.info("******PageSize="+String.valueOf(pageInfo.getPageSize()));
        return pageInfo;
    }
    @CrossOrigin(origins = "*")
    @RequestMapping(value = "/getTCmStaffById", method = RequestMethod.GET)
    public TCmStaff getTCmStaffById(@RequestParam String staffId) {
        TCmStaff pojo = tCmStaffMapper.selectByPrimaryKey(staffId);
        if (pojo == null) {
            return null;
        }
        return pojo;
    }
    @CrossOrigin(origins = "*")
    @RequestMapping(value = "/insertTCmStaff", method = RequestMethod.POST)
    public ErrorJsonBean insert(@RequestBody TCmStaff param) {

        param.setLastModifydate(new Date());
        TCmUserAccount tCmUserAccount=new TCmUserAccount();
//        param.se(new Date());
        tCmUserAccount.setUserAccount(param.getStaffAccount());
        tCmUserAccount.setPasswd(param.getStaffPwd());
        // 1 供应商2 员工3  管理员帐号
        tCmUserAccount.setAuthType(1);
        short sh=0;
        int x= tCmStaffMapper.insert(param);
        tCmUserAccount.setLockFlag(sh);
        tCmUserAccount.setOperateId(param.getStaffId());
        tCmUserAccount.setUserAccountId(UUID.randomUUID().toString().replace("-", ""));
        tCmUserAccount.setOpenTime(param.getLastModifydate());
        x=tCmUserAccountMapper.insert(tCmUserAccount);
        ErrorJsonBean errorJsonBean=new ErrorJsonBean();
        if( x > 0){
        }else{
            errorJsonBean.fail(Integer.parseInt(ErrorCode.ECODE_SYSFAIL),ErrorCode.getEmsg(ErrorCode.ECODE_SYSFAIL));
        }
        return errorJsonBean;



    }
    @CrossOrigin(origins = "*")
    @RequestMapping(value = "/updateTCmStaff", method = RequestMethod.POST)
    public Boolean update(@RequestBody TCmStaff param) {
        param.setLastModifydate(new Date());
        int x= tCmStaffMapper.updateByPrimaryKey(param);
        return x > 0;
    }

    /**
     * 采用物理删除 delFlag
     * @param param
     * @return
     */
    @CrossOrigin(origins = "*")
    @RequestMapping(value = "/deleteTCmStaff", method = RequestMethod.POST)
    public Boolean delete(@RequestBody TCmStaff param) {
        short delflag=1;
        param.setDelFlag(delflag);
        param.setLastModifydate(new Date());
        int x= tCmStaffMapper.updateByPrimaryKey(param);
        return x > 0;
    }
    // TODO add method
}